package com.ruowei.modules.sys.utils;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelCreateUtils {

    /**
     * @param workbook
     * @param fontSize
     * @param isBold
     * @param isCenter
     * @apiNote 创建单元格格式
     * @author 董兴
     * @date 2020-02-12
     */
    public static XSSFCellStyle createCellStyle(XSSFWorkbook workbook, short fontSize, Boolean isBold, Boolean isCenter) {
        XSSFCellStyle style = workbook.createCellStyle();

        style.setBorderBottom(BorderStyle.THIN);//下边框
        style.setBorderLeft(BorderStyle.THIN);//左边框
        style.setBorderRight(BorderStyle.THIN);//右边框
        style.setBorderTop(BorderStyle.THIN);//上边框

        //是否水平居中
        if (isCenter) {
            style.setAlignment(HorizontalAlignment.CENTER);
        }
        //垂直居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        //创建字体
        XSSFFont font = workbook.createFont();
        //是否加粗
        if (isBold) {
            font.setBold(true);
        }
        font.setFontHeightInPoints(fontSize);
        //加载字体
        style.setFont(font);
        //自动换行
        style.setWrapText(true);
        return style;
    }

    /**
     * @param sheet
     * @param cellAddresses
     * @apiNote 合并单元格格式
     * @author 董兴
     * @date 2020-02-12
     */
    public static void createCellsStyle(XSSFSheet sheet, CellRangeAddress cellAddresses) {
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellAddresses, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cellAddresses, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cellAddresses, sheet);
        RegionUtil.setBorderTop(BorderStyle.THIN, cellAddresses, sheet);
    }
}
